package lime;

/**
 * <p>Title: Lime</p>
 * <p>Description: Lime</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: DII - DISMI</p>
 * @author Marco Mamei
 * @version 1.0
 */

import java.io.*;
import emulator.*;

public class Start
{
 private static String configFile = "E:\\CODE\\TOTA\\ConfigFile\\lime.xml";
 private static String configFile = "E:\\CODE\\TOTA\\ConfigFile\\limeManetFromFileEXP.xml";
 private static final int EMULATOR = 0;
 private static final int REAL = 1;

 private static int mode = EMULATOR;

 public static void main(String[] args)
 {
  switch(mode)
  {
   case EMULATOR  : startEmulator(configFile);
                    break;

   case REAL      : startReal();
                    break;
  }
 }


 /*****************************************************************************/
 /*                         START EMULATOR                                    */
 /*****************************************************************************/

 private static void startEmulator(String configFile)
 {
  int NUM_PARAM = 22;
  String[] args = new String[NUM_PARAM];
  try
  {
   BufferedReader in = new BufferedReader(new FileReader(configFile));
   int index = 0;
   String line;
   while( (line = in.readLine()) != null)
   {
    if (!line.trim().startsWith("<")) // skip xml tags
    {
     args[index] = line.trim();
     index++;
    }
   }
   in.close();
  }
  catch(Exception e){e.printStackTrace();}

  Emulator.startFromCommandLine(args);
 }

 /*****************************************************************************/
 /*                               START REAL                                  */
 /*****************************************************************************/

 private static void startReal()
 {
  /* here is a thread synchronization problem: altough all the threads within
  the peer are properly synchronized to call methods on the upper layer in a
  sequntial fashion, this thread - the main one - calls the step method on the
  agent. The peer thread and the main thread are not synchronized!

  I have to create a unique synchonization object and use that for sync
  */
  Object main_sync = new int[1];

  real802_11.Peer peer = new real802_11.Peer(main_sync);
  System.out.println("il peer creato ha valore: "+peer);
  /* install agent on the peer */
  lime.agent.AgentLime ag = new lime.agent.AgentLime(peer);
  /* start executing the agent */
  ag.showAgentFrame();
  int time = 0;
  while(true)
  {
   synchronized (main_sync)
   {
    ag.step(time);
    try{Thread.currentThread().sleep(100);}catch(Exception e){e.printStackTrace();}
    time++;
   }
  }
 }
}